package com.cxg.fairy.ec.main.personal.balance;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.money.MoneyUtil;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/28.
 */

public class BalanceDelegate extends LatteDelegate{

    @BindView(R2.id.tv_current_balance)
    AppCompatTextView tvBalance = null;

    @BindView(R2.id.tab_layout)
    TabLayout mTabLayout = null;

    @BindView(R2.id.view_pager)
    ViewPager mViewPager = null;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_balance_tab;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initTabLayout();
        initData();
        initPager();
    }

    private void initTabLayout() {
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getContext(), R.color.favorListTag));
        mTabLayout.setTabTextColors(Color.parseColor("#565656"),Color.BLACK);
        mTabLayout.setBackgroundColor(Color.WHITE);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initPager() {
        final PagerAdapter adapter = new BalanceTabPagerAdapter(getFragmentManager());
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(adapter);
    }

    private void initData() {
        RestClient.builder()
                .url("getCurrentMsBalance")
                .loader(getContext())
                .params("uuid", AccountManager.getSignUUID())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                       LatteLogger.e("response=",response);
                       int balance =  JSON.parseObject(response).getJSONObject("data").getInteger("balance");
                       tvBalance.setText("¥"+ MoneyUtil.formatMoney(balance));
                    }
                })
                .build()
                .post();

    }

}
